﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SCRUM.Core.DAO;

/// <summary>
/// Widget for making work progress.
/// </summary>
public partial class WorkProgress : WidgetBase
{
	/// <summary>
	/// Total hours count.
	/// </summary>
	private double totalHours;

	/// <summary>
	/// Gets or sets a value indicating whether [user has no tasks].
	/// </summary>
	/// <value><c>true</c> if [user has no tasks]; otherwise, <c>false</c>.</value>
	protected bool UserHasNoTasks { get; set; }

    /// <summary>
    /// Gets the name of the widget. It must be exactly the same as the folder that contains the widget.
    /// </summary>
    /// <value></value>
    public override string Name
    {
        get { return "WorkProgress"; }
    }

    /// <summary>
    /// Gets the title. Title is shown in the header of the widget container.
    /// </summary>
    /// <value>The title.</value>
    public override string Title
    {
        get { return (string)GetGlobalResourceObject("WidgetTypeTitle", "WorkProgress"); }
    }

    /// <summary>
    /// Gets a value indicating whether this instance is editable.
    /// </summary>
    /// <value>
    /// 	<c>true</c> if this instance is editable; otherwise, <c>false</c>.
    /// </value>
    public override bool IsEditable
    {
        get { return false; }
    }

    /// <summary>
    /// This method works as a substitute for Page_Load. You should use this method for
    /// data binding etc. instead of Page_Load.
    /// </summary>
    public override void LoadWidget()
    {
		if (!IsPostBack)
		{
			this.DateInputFrom.SeledtedDate = DateTime.Now.AddDays(-7);
			this.DateInputTo.SeledtedDate = DateTime.Now;
		}
		this.BindGridView();
    }

	/// <summary>
	/// Raises the <see cref="E:System.Web.UI.Control.Init"/> event.
	/// </summary>
	/// <param name="e">An <see cref="T:System.EventArgs"/> object that contains the event data.</param>
	protected override void OnInit(EventArgs e)
	{
		base.OnInit(e);
		this.WorkProgressDetail.NoTaskAssigned += new EventHandler(NoTaskAssignedUser);
		this.totalHours = 0;
	}

	/// <summary>
	/// Binds the grid view.
	/// </summary>
	/// <param name="from">From.</param>
	/// <param name="to">To.</param>
	protected void BindGridView()
	{
		this.GridView.DataSource = DAOWorkProgress.GetProgressForUser(DAOUser.GetLoggedUser(), this.DateInputFrom.SeledtedDate, this.DateInputTo.SeledtedDate);
		this.GridView.DataBind();
	}

    /// <summary>
    /// Shows the edit.
    /// </summary>
    public override void ShowEdit()
    {
        throw new NotImplementedException();
    }

    /// <summary>
    /// Hides the edit.
    /// </summary>
    public override void HideEdit()
    {
        throw new NotImplementedException();
    }

    /// <summary>
    /// Handles the PreRender event of the Page control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void Page_PreRender(object sender, EventArgs e)
    {
		if (!IsPostBack)
		{
			this.DateInputFrom.SeledtedDate = DateTime.Now.AddDays(-7);
			this.DateInputTo.SeledtedDate = DateTime.Now;
		}
		this.BindGridView();
		if ((this.GridView.DataSource == null) || (this.GridView.DataSource as IList<SCRUM.Core.BusinessObjects.WorkProgress>).Count == 0)
		{
			this.ShowOperationResult(MessageType.Warning, "no_data_for_filters");
		}

		if (this.UserHasNoTasks)
		{
			this.WorkProgressDetail.Visible = false;
			this.ShowOperationResult(MessageType.Error, "UserHasNoTasks");
		}
    }

	/// <summary>
	/// When row bounds data.
	/// </summary>
	/// <param name="sender">The sender.</param>
	/// <param name="e">The <see cref="System.Web.UI.WebControls.GridViewRowEventArgs"/> instance containing the event data.</param>
	protected void GridViewRowDataBound(object sender, GridViewRowEventArgs e)
	{
		if (e.Row.RowType == DataControlRowType.DataRow)
		{
			SCRUM.Core.BusinessObjects.WorkProgress workProgress = e.Row.DataItem as SCRUM.Core.BusinessObjects.WorkProgress;
			double hours = DAOUser.StoryPointsToHours(workProgress.StoryPoints);
			Literal literalHours = e.Row.FindControl("LiteralHours") as Literal;
			literalHours.Text = String.Format("{0:0.##}", hours);
			this.totalHours += hours;
		}
		else if (e.Row.RowType == DataControlRowType.Footer)
		{
			e.Row.Cells[2].Text = (string)GetLocalResourceObject("Total");
			e.Row.Cells[3].Text = String.Format("{0:0.##}", this.totalHours);
			this.totalHours = 0;
		}
	}

	/// <summary>
	/// Handles the event when index changed.
	/// </summary>
	/// <param name="sender">The sender.</param>
	/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
	protected void GridViewSelectedIndexChanged(object sender, EventArgs e)
	{
		this.WorkProgressDetail.ShowEditWorkProgress(
			DAOWorkProgress.GetProgressByID(Int32.Parse(this.GridView.SelectedDataKey.Value.ToString())));
	}

	/// <summary>
	/// Deletes the work progress.
	/// </summary>
	/// <param name="sender">The sender.</param>
	/// <param name="e">The <see cref="System.Web.UI.WebControls.CommandEventArgs"/> instance containing the event data.</param>
	protected void DeleteWorkProgress(object sender, CommandEventArgs e)
	{
		DAOWorkProgress.DeleteProgress(Int32.Parse((string)e.CommandArgument));
		BindGridView();
	}

	/// <summary>
	/// Creates the work progress.
	/// </summary>
	/// <param name="sender">The sender.</param>
	/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
	protected void CreateWorkProgress(object sender, EventArgs e)
	{
		this.WorkProgressDetail.ShowCreateWorkProgress();
	}

	/// <summary>
	/// Applies the filters.
	/// </summary>
	/// <param name="sender">The sender.</param>
	/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
	protected void ApplyFilters(object sender, EventArgs e)
	{
		this.BindGridView();
		this.GridView.SelectedIndex = -1;
	}

	/// <summary>
	/// Executes when there is no associated task for user.
	/// </summary>
	/// <param name="sender">The sender.</param>
	/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
	protected void NoTaskAssignedUser(object sender, EventArgs e)
	{
		this.UserHasNoTasks = true;
	}
}
